<?php


namespace app\app\controller;

//
use app\model\AdminReward;
use app\util\ReturnCode;
use think\exception\DbException;
use think\facade\Cache;

class Reward extends Base
{
    //获取我的推广
    public function getMyReward(){
        $limit = $this->request->get('size', config('apiadmin.ADMIN_LIST_DEFAULT'));
        $start = $this->request->get('page', 1);
        $status= $this->request->get('status');
        $model = new AdminReward();
        $where = ['uid'=>$this->userInfo['uid'],'status'=>$status];
        $where = $this->addTid($where);
        try {
            $listObj = $model->where($where)->order('create_time DESC')->paginate($limit, false, ['page' => $start])->toArray();
            return $this->buildSuccess([
                'list' => $listObj['data'],
                'count' => $listObj['total']
            ]);
        } catch (DbException $e) {
        }
        return $this->buildFailed(ReturnCode::FAILED, '获取失败');
    }
    //获取排行
    public function getRank(){
        $limit = $this->request->get('size', config('apiadmin.ADMIN_LIST_DEFAULT'));
        $start = $this->request->get('page', 1);
        $type= $this->request->get('type');
        $cache = cache('rank_' . $this->getUserTid().'_'.$type);
        if($cache){
            $listObj = $cache;
            return $this->buildSuccess([
                'list' => $listObj['data'],
                'count' => $listObj['total']
            ]);
        }
        $model = new AdminReward();
        $where = ['uid'=>$this->userInfo['uid']];
        $startTime = null;
        switch ($type){
            case 0:
                break;
            case 1://本日
                $model->whereTime('create_time','today');
                break;
            case 2://本周
                $model->whereTime('create_time','week');
                break;
            case 3://本月
                $model->whereTime('create_time','month');
                break;
        }

        $where = $this->addTid($where);
        try {
            $listObj = $model->group('uid')->where($where)->order('create_time DESC')->field('SUM(money) as total,uid')->paginate($limit, false, ['page' => $start])->each(function($value,$key){
                $value->wxUserInfo;
            })->toArray();
            Cache::set('rank_' . $this->getUserTid().'_'.$type,$listObj,60*5);
            return $this->buildSuccess([
                'list' => $listObj['data'],
                'count' => $listObj['total']
            ]);
        } catch (DbException $e) {
        }
        return $this->buildFailed(ReturnCode::FAILED, '获取失败');
    }

}